package kun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * java中的游戏面板：JPanel
 * 自定义游戏面板
 * 1.写一个类，继承JPanel
 * 2.写一个构造方法，初始化面板属性
 * <p>
 * 画图的方法
 * 1.在类中定义图片，并取名
 * 2.在构造方法中，调用工具初始化图片
 * 3，在画图方法，paint中。画图
 */
public class GamePanel extends JPanel {
    //1.定义背景图
    BufferedImage bj;//背景图

    //创建英雄机对象
    Hero hero = new Hero();

    //创建敌机
    //Ep ep = new Ep();
    List<Ep> eps = new ArrayList<Ep>();

    //创建英雄机弹药库
    List<Fire> fires = new ArrayList<Fire>();

    //定义分数
    int score;

    //设置游戏的开关
    boolean gameOver;

    int power = 1;

    /**
     * 开始游戏的方法
     */

    public void action() {
        //创建并启动线程，控制游戏场景中的物体移动
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    if (!gameOver) {
                        epEnter();
                        epMove();
                        fire();
                        fireMove();
                        fireEp();
                        hitEp();
                    }
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    repaint();
                }
            }
        }.start();
    }

    //检测敌机是否撞到英雄机
    private void hitEp() {
        for (int i = 0; i < eps.size(); i++) {
            Ep e = eps.get(i);
            if (e.hitBy(hero)) {
                eps.remove(e);
                hero.hp--;
                power = 1;
                score += 10;
                if (hero.hp <= 0) {
                    gameOver = true;
                }
            }
        }
    }

    //检测是否击中敌机
    private void fireEp() {
        for (int i = 0; i < fires.size(); i++) {
            Fire f = fires.get(i);
            bang(f);
        }

    }

    //判断是否击中敌机
    private void bang(Fire f) {
        for (int i = 0; i < eps.size(); i++) {
            Ep e = eps.get(i);
            if (e.fireBy(f)) {
                e.hp--;
                if (e.hp <= 0) {
                    if (e.type == 5) {
                        power++;
                        if(power>3){
                            hero.hp++;
                            power = 3;
                        }
                    }
                    eps.remove(e);
                    score += 10;
                }
                fires.remove(f);
            }
        }
    }

    //子弹移动
    private void fireMove() {
        for (int i = 0; i < fires.size(); i++) {
            Fire f = fires.get(i);
            f.move();
        }
    }

    //发射子弹的方法
    int findex =0 ;

    private void fire() {
        findex++;
        if (findex >= 5) {
            if(power==1){
                Fire fire2 = new Fire(hero.x + hero.w / 2, hero.y, 1);
                fires.add(fire2);
                findex = 0;
            }else if(power==2){
                Fire fire1 = new Fire(hero.x, hero.y, 0);
                fires.add(fire1);
                Fire fire3 = new Fire(hero.x + hero.w, hero.y, 2);
                fires.add(fire3);
                findex = 0;
            }else if(power==3){
                Fire fire1 = new Fire(hero.x, hero.y, 0);
                fires.add(fire1);
                Fire fire2 = new Fire(hero.x + hero.w / 2, hero.y, 1);
                fires.add(fire2);
                Fire fire3 = new Fire(hero.x + hero.w, hero.y, 2);
                fires.add(fire3);
                findex = 0;
            }

        }

    }

    //敌机移动的方法
    private void epMove() {
        for (int i = 0; i < eps.size(); i++) {
            Ep ep = eps.get(i);
            ep.move();
        }
    }

    int index = 0;

    private void epEnter() {
        index++;
        if (index >= 25) {
            Ep ep = new Ep();
            eps.add(ep);
            index = 0;
        }
    }

    /**
     * 构造方法：确定面板的特点
     */
    public GamePanel(GameFrame frame) {
        //设置背景
        setBackground(Color.black);

        //初始化图片
        bj = App.getImg("/kun/img/bj1.jpg");


        //鼠标监听器
        MouseAdapter adapter = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //System.out.println("点击了鼠标");
                if (gameOver) {
                    hero = new Hero();
                    eps = new ArrayList<Ep>();
                    fires = new ArrayList<Fire>();
                    gameOver = false;
                    score = 0;
                    power = 1;
                    Random rd = new Random();
                    int index = rd.nextInt(3) + 1;
                    bj = App.getImg("/kun/img/bj" + index + ".jpg");
                    repaint();
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                //System.out.println("移动了鼠标");

                int mx = e.getX();
                int my = e.getY();

                if (!gameOver) {
                    hero.moveToMouse(mx, my);
                }
                repaint();
            }
        };

        addMouseListener(adapter);
        addMouseMotionListener(adapter);

        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("键盘监听器运行");

                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP && hero.y > 0) {
                    hero.moveUp();
                } else if (keyCode == KeyEvent.VK_DOWN && hero.y < 770 - hero.h - 36) {
                    hero.moveDown();
                } else if (keyCode == KeyEvent.VK_LEFT && hero.x > 0) {
                    hero.moveLeft();
                } else if (keyCode == KeyEvent.VK_RIGHT && hero.x < 510 - hero.w) {
                    hero.moveRight();
                }
                repaint();
            }
        };
        frame.addKeyListener(keyAdapter);
    }

    /**
     * 专用的画图方法
     * Graphics g  画笔
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画图片
        //g.drawImage
        g.drawImage(bj, 0, 0, 510, 770, null);

        g.drawImage(hero.img, hero.x, hero.y, hero.w, hero.h, null);

        for (int i = 0; i < eps.size(); i++) {
            Ep ep = eps.get(i);
            g.drawImage(ep.img, ep.x, ep.y, ep.w, ep.h, null);
        }
        for (int i = 0; i < fires.size(); i++) {
            Fire fire = fires.get(i);
            g.drawImage(fire.img, fire.x, fire.y, fire.w, fire.h, null);
        }
        g.setColor(Color.WHITE);
        g.setFont(new Font("楷体", Font.BOLD, 20));
        g.drawString("分数:" + score, 10, 30);
        g.drawString("生命:", 110, 30);

        for (int i = 0; i < hero.hp; i++) {
            g.drawImage(hero.img, 170 + i * 35, 5, 44, 30, null);
        }
        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("楷体", Font.BOLD, 35));
            g.drawString("GAMEOVER!菜鸡", 140, 300);
            g.setColor(Color.GREEN);
            g.setFont(new Font("楷体", Font.BOLD, 20));
            g.drawString("点击屏幕重新开始！小垃圾", 140, 400);
        }
    }
}
